<template>
  <div class="dashboard-container">
    <div class="app-container">
      <Page-tools :show-before="true">
        <span slot="before">共{{ total }}条记录</span>
        <template slot="after">
          <el-button size="small" type="warning" @click="$router.push('/import')">导入</el-button>
          <el-button size="small" type="danger">导出</el-button>
          <el-button size="small" type="primary" @click="showDialog = true">新增员工</el-button>
        </template>
      </Page-tools>
      <!-- 放置表格和分页 -->
      <el-card>
        <el-table border :data="getEmployeeList">
          <el-table-column label="序号" type="index">
            <template #default="{$index}">
              {{ (page.page -1) * page.size + ($index+1) }}
            </template>
          </el-table-column>
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment" :formatter="formatterFn" />
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry">
            <template #default="{row}">
              {{ row.timeOfEntry | formatDate }}
            </template>
          </el-table-column>
          <el-table-column label="账户状态" prop="enableState">
            <template #default="{row}">
              <el-switch
                v-model="row.enableState"
                :active-value="1"
                :inactive-value="0"
                disabled
              />
            </template>
          </el-table-column>

          <el-table-column label="操作" fixed="right" width="280">
            <template #default="{row}">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small" @click="delBen(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            background
            layout="prev, pager, next"
            :page-size="page.size"
            :total="total"
            @current-change="currentChange"
          />
        </el-row>
      </el-card>
      <!-- 弹出框 -->
      <AddEmployee
        :show-dialog.sync="showDialog"
        @add="getEmployeeListFn"
      />
    </div>
  </div>
</template>

<script>
// 获取员工数据列表
import { getEmployeeList, delEmployee } from '@/api/employees'
// 引入玫举数据
import EmployeeEnum from '@/api/constant/employees'
// 引入组件
import AddEmployee from './components/add-employee.vue'
export default {
  components: {
    AddEmployee
  },
  data() {
    return {
      getEmployeeList: [], // 员工数据列表
      page: {
        page: 1, // 当前页码
        size: 5 // 每页条数
      },
      total: 0, // 总数
      showDialog: false,
      rules: { // 效验
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    // 员工数据列表
    this.getEmployeeListFn()
  },
  methods: {
    async  getEmployeeListFn() {
      const { rows, total } = await getEmployeeList(this.page)
      // console.log(rows)
      this.getEmployeeList = rows
      this.total = total
    },
    // 分页
    currentChange(currentPage) {
      // 用户点击的页码赋值给当前页码
      this.page.page = currentPage
      // 刷新页面
      this.getEmployeeListFn()
    },
    // 删除
    async  delBen(id) {
      // 二次确认
      await this.$confirm('确定要删除吗？')
      // 发送请求
      await delEmployee(id)
      // 提示
      this.$message.success('删除成功')
      // 刷新页面
      this.getEmployeeListFn()
    },
    // 聘用形式
    formatterFn(row, column, cellValue) {
      // console.log(99, cellValue)
      const obj = EmployeeEnum.hireType.find(item => Number(item.id) === cellValue)
      // console.log(100, obj)
      return obj ? obj.value : '未知'
    }
  }
}
</script>

<style>

</style>
